package net.sudot.commons.pagination;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页查询分页参数封装
 *
 * @author 唐家林
 * @time 2016年3月30日
 */
public class PageRequest implements Pageable {
    /** 最大每页数据条数 */
    protected static final int MAX_PAGE_SIZE = 1000;
    /** 默认每页数据条数 */
    protected static final int DEFAULT_PAGE_SIZE = 20;
    /** 默认当前页码 */
    protected static final int DEFAULT_PAGE_NUMBER = 1;

    /** 每页数据条数 */
    protected int rows = DEFAULT_PAGE_SIZE;
    /** 页码 */
    protected int page = DEFAULT_PAGE_NUMBER;

    /** 查询条件集 */
    protected List<QueryCondition> queryConditions;

    public PageRequest() {
    }

    public PageRequest(int page) {
        setPage(page);
    }

    public PageRequest(int page, int rows) {
        setPage(page);
        setRows(rows);
    }

    @Override
    public int getRows() {
        return rows;
    }

    @Override
    public Pageable setRows(int rows) {
        this.rows = rows < DEFAULT_PAGE_SIZE || rows > MAX_PAGE_SIZE ? DEFAULT_PAGE_SIZE : rows;
        return this;
    }

    @Override
    public int getPage() {
        return page;
    }

    @Override
    public Pageable setPage(int page) {
        this.page = page < DEFAULT_PAGE_NUMBER ? DEFAULT_PAGE_NUMBER : page;
        return this;
    }

    @Override
    public List<QueryCondition> getQueryConditions() {
        return queryConditions == null ? (queryConditions = new ArrayList<>()) : queryConditions;
    }

    @Override
    public Pageable setQueryConditions(List<QueryCondition> queryConditions) {
        this.queryConditions = queryConditions;
        return this;
    }

}
